home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / CW GUSI 1.6.4 / include / GUSI_P.h < prev    next >
C/C++ Source or Header  |  1995-11-05  |  11KB  |  440 lines

  1. /*********************************************************************
  2. Project    :    GUSI                -    Grand Unified Socket Interface
  3. File        :    GUSI_P.h            -    Private stuff
  4. Author    :    Matthias Neeracher
  5. Language    :    MPW C/C++
  6.  
  7. $Log: GUSI_P.h,v $
  8. Revision 1.3  1994/12/31  01:30:26  neeri
  9. Reorganize filename dispatching.
  10.  
  11. Revision 1.2  1994/08/10  00:41:05  neeri
  12. Sanitized for universal headers.
  13.  
  14. Revision 1.1  1994/02/25  02:57:01  neeri
  15. Initial revision
  16.  
  17. Revision 0.22  1993/07/17  00:00:00  neeri
  18. GUSIRingBuffer::proc -> defproc
  19.  
  20. Revision 0.21  1993/07/17  00:00:00  neeri
  21. GUSIO_MAX_DOMAIN -> AF_MAX
  22.  
  23. Revision 0.20  1993/06/27  00:00:00  neeri
  24. Socket::{pre,post}_select
  25.  
  26. Revision 0.19  1993/06/27  00:00:00  neeri
  27. Socket::ftruncate
  28.  
  29. Revision 0.18  1993/02/09  00:00:00  neeri
  30. Socket::lurking, Socket::lurkdescr
  31.  
  32. Revision 0.17  1993/01/31  00:00:00  neeri
  33. GUSIConfiguration::daemon
  34.  
  35. Revision 0.16  1993/01/17  00:00:00  neeri
  36. Destructors for Socketdomain
  37.  
  38. Revision 0.15  1993/01/17  00:00:00  neeri
  39. SAFESPIN
  40.  
  41. Revision 0.14  1993/01/03  00:00:00  neeri
  42. GUSIConfig
  43.  
  44. Revision 0.13  1992/09/24  00:00:00  neeri
  45. Include GUSIRsrc_P.h
  46.  
  47. Revision 0.12  1992/09/13  00:00:00  neeri
  48. SPINVOID didn't return
  49.  
  50. Revision 0.11  1992/08/30  00:00:00  neeri
  51. AppleTalkIdentity()
  52.  
  53. Revision 0.10  1992/08/03  00:00:00  neeri
  54. RingBuffer
  55.  
  56. Revision 0.9  1992/07/30  00:00:00  neeri
  57. Initializer Features
  58.  
  59. Revision 0.8  1992/07/26  00:00:00  neeri
  60. UnixSockets.choose()
  61.  
  62. Revision 0.7  1992/07/13  00:00:00  neeri
  63. Make AppleTalkSockets global
  64.  
  65. Revision 0.6  1992/06/27  00:00:00  neeri
  66. choose(), hasNewSF
  67.  
  68. Revision 0.5  1992/06/07  00:00:00  neeri
  69. Feature
  70.  
  71. Revision 0.4  1992/05/21  00:00:00  neeri
  72. Implemented select()
  73.  
  74. Revision 0.3  1992/04/19  00:00:00  neeri
  75. C++ rewrite
  76.  
  77. Revision 0.2  1992/04/18  00:00:00  neeri
  78. changed read/write/send/recv dispatchers
  79.  
  80. Revision 0.1  1992/04/18  00:00:00  neeri
  81. ppc Domain
  82.  
  83. *********************************************************************/
  84.  
  85. #ifndef __GUSI_P__
  86. #define __GUSI_P__
  87.  
  88. #define __useAppleExts__
  89.  
  90. #include <GUSI.h>
  91. #include <GUSIRsrc_P.h>
  92. #include <TFileSpec.h>
  93.  
  94.  
  95. #include <sys/errno.h>
  96. #include <sys/uio.h>
  97. #include <sys/socket.h>
  98.  
  99. extern "C" {
  100.  
  101. #include <stdio.h>
  102. #include <string.h>
  103.  
  104. int         GUSI_error(int err);
  105. void *    GUSI_error_nil(int err);
  106. }
  107.  
  108. #include <Memory.h>
  109. #include <GestaltEqu.h>
  110. #include <Traps.h>
  111. #include <AppleEvents.h>
  112.  
  113. #ifndef GUSI_NO_UNIVERSAL
  114. #include <MixedMode.h>
  115. #endif
  116.  
  117. #if GENERATING68K
  118. #pragma segment GUSI
  119. #endif
  120.  
  121. #define GUSI_MAX_DOMAIN            AF_MAX
  122. #define DEFAULT_BUFFER_SIZE    4096
  123.  
  124. /*
  125.  *    In use and shutdown status.
  126.  */
  127. #define    SOCK_STATUS_USED        0x1        /* Used socket table entry */
  128. #define    SOCK_STATUS_NOREAD    0x2        /* No more reading allowed from socket */
  129. #define    SOCK_STATUS_NOWRITE    0x4        /* No more writing allowed to socket */
  130.  
  131. /*
  132.  *    Socket connection states.
  133.  */
  134. #define    SOCK_STATE_NO_STREAM        0    /* Socket doesn't have a MacTCP stream yet */
  135. #define    SOCK_STATE_UNCONNECTED    1    /* Socket is unconnected. */
  136. #define    SOCK_STATE_LISTENING        2    /* Socket is listening for connection. */
  137. #define    SOCK_STATE_LIS_CON        3    /* Socket is in transition from listen to connected. */
  138. #define    SOCK_STATE_CONNECTING    4    /* Socket is initiating a connection. */
  139. #define    SOCK_STATE_CONNECTED        5    /* Socket is connected. */
  140. #define    SOCK_STATE_CLOSING      6    /* Socket is closing */
  141. #define    SOCK_STATE_LIS_CLOSE    7    /* Socket closed while listening */
  142.  
  143. #define        min(a,b)                ( (a) < (b) ? (a) : (b))
  144. #define        max(a,b)                ( (a) > (b) ? (a) : (b))
  145.  
  146. extern GUSISpinFn GUSISpin;
  147. extern "C" int GUSIDefaultSpin(spin_msg, long);
  148. extern int GUSICheckAlarm();
  149.  
  150. #define GUSI_INTERRUPT(mesg,param)    (GUSICheckAlarm() || (GUSISpin && (*GUSISpin)(mesg,param)))
  151.  
  152. /* SPIN returns a -1 on user cancel for fn returning integers */
  153. #define        SPIN(cond,mesg,param)                            \
  154.                     do {                                                \
  155.                         if (GUSI_INTERRUPT(mesg,param))        \
  156.                             return GUSI_error(EINTR);            \
  157.                     } while(cond)
  158.  
  159. /* SPINP returns a NULL on user cancel, for fn returning pointers */                
  160. #define        SPINP(cond,mesg,param)                            \
  161.                     do {                                                \
  162.                         if (GUSI_INTERRUPT(mesg,param)) {    \
  163.                             GUSI_error(EINTR);                    \
  164.                             return NULL;                            \
  165.                         }                                                \
  166.                     } while(cond)
  167.  
  168. /* SPINVOID just returns on user cancel, for fn returning void */                
  169. #define        SPINVOID(cond,mesg,param)                        \
  170.                     do {                                                \
  171.                         if (GUSI_INTERRUPT(mesg,param)) {    \
  172.                                 GUSI_error(EINTR);                \
  173.                                 return;                                \
  174.                             }                                            \
  175.                     } while(cond)
  176.                     
  177. /* SAFESPIN doesn't return, you have to check errno */                
  178. #define        SAFESPIN(cond,mesg,param)                        \
  179.                     do {                                                \
  180.                         if (GUSI_INTERRUPT(mesg,param)) {    \
  181.                             GUSI_error(EINTR);                    \
  182.                             break;                                    \
  183.                         } else                                        \
  184.                             errno = 0;                                \
  185.                     } while(cond)
  186.                     
  187. class SocketTable;
  188.  
  189. #if PRAGMA_ALIGN_SUPPORTED
  190. #pragma options align=mac68k
  191. #endif
  192.  
  193. class Socket {
  194.     friend class SocketTable;
  195.     
  196.     short            refCount;
  197. protected:
  198.                     Socket();
  199. public:
  200.     virtual int    bind(void * name, int namelen);
  201.     virtual int connect(void * address, int addrlen);
  202.     virtual int listen(int qlen);
  203.     virtual Socket * accept(void * address, int * addrlen);
  204.     virtual int    read(void * buffer, int buflen);
  205.     virtual int write(void * buffer, int buflen);
  206.     virtual int recvfrom(void * buffer, int buflen, int flags, void * from, int * fromlen);
  207.     virtual int sendto(void * buffer, int buflen, int flags, void * to, int tolen);
  208.     virtual int getsockname(void * name, int * namelen);
  209.     virtual int getpeername(void * name, int * namelen);
  210.     virtual int getsockopt(int level, int optname, void *optval, int * optlen);
  211.     virtual int setsockopt(int level, int optname, void *optval, int optlen);
  212.     virtual int    fcntl(unsigned int cmd, int arg);
  213.     virtual int    ioctl(unsigned int request, void *argp);
  214.     virtual int    fstat(struct stat * buf);
  215.     virtual long lseek(long offset, int whence);
  216.     virtual int ftruncate(long offset);
  217.     virtual int    isatty();
  218.     virtual int shutdown(int how);
  219.     virtual void pre_select(Boolean wantRead, Boolean wantWrite, Boolean wantExcept);
  220.     virtual int select(Boolean * canRead, Boolean * canWrite, Boolean * exception);
  221.     virtual void post_select(Boolean wantRead, Boolean wantWrite, Boolean wantExcept);
  222.     virtual         ~Socket();
  223.     
  224.     void operator++()    {    ++refCount;                            }
  225.     void operator--()    {    if (!--refCount) delete this;    }
  226. };
  227.  
  228.  
  229. #if PRAGMA_ALIGN_SUPPORTED
  230. #pragma options align=reset
  231. #endif
  232.  
  233. class SocketDomain {
  234.     static SocketDomain *        domains[GUSI_MAX_DOMAIN];
  235.     static ProcessSerialNumber    process;
  236. protected:
  237.     SocketDomain(int domain);
  238.     virtual ~SocketDomain();
  239. public:
  240.     inline static SocketDomain *    Domain(int domain);
  241.     static void Ready();
  242.     
  243.     // Optionally override the following
  244.     
  245.     virtual Socket * socket(int type, short protocol);
  246.     
  247.     // Optionally define the following
  248.     
  249.     virtual int choose(
  250.                         int         type, 
  251.                         char *     prompt, 
  252.                         void *     constraint,        
  253.                         int         flags,
  254.                          void *     name, 
  255.                         int *     namelen);
  256.     
  257.     // Never override the following
  258.     
  259.     void DontStrip();
  260. };
  261.  
  262. class SocketTable {
  263.     Socket *    sockets[GUSI_MAX_FD];
  264.     Boolean    needsConsole;
  265. public:
  266.     SocketTable();
  267.     ~SocketTable();
  268.     
  269.     void        InitConsole();
  270.     int        Install(Socket * sock, int start = 0);
  271.     int        Remove(int fd);
  272.     Socket * operator[](int fd);
  273. };
  274.  
  275. struct GUSISuffix {
  276.     char         suffix[4];
  277.     OSType    suffType;
  278.     OSType    suffCreator;
  279. };
  280.  
  281. #if PRAGMA_ALIGN_SUPPORTED
  282. #pragma options align=mac68k
  283. #endif
  284.  
  285. //
  286. // I learned the hard way not to rely on bit field alignments
  287. //
  288.  
  289. struct GUSIConfigRsrc {
  290.     OSType            defaultType;
  291.     OSType            defaultCreator;
  292.     
  293.     char                autoSpin;
  294.     unsigned char    flags;
  295.     
  296.     OSType            version;
  297.     short                numSuffices;
  298.     GUSISuffix         suffices[1];
  299. };
  300.  
  301. #if PRAGMA_ALIGN_SUPPORTED
  302. #pragma options align=reset
  303. #endif
  304.  
  305. struct GUSIConfiguration {
  306.     OSType            defaultType;
  307.     OSType            defaultCreator;
  308.     
  309.     char                autoSpin;
  310.     
  311.     Boolean             noChdir;        // Set current directory without chdir()
  312.     Boolean             accurStat;    // Return # of subdirectories + 2 in st_nlink
  313.     Boolean             hasConsole;    // Do we have our own console ?
  314.     
  315.     OSType            version;
  316.     short                numSuffices;
  317.     GUSISuffix *    suffices;
  318.     
  319.     GUSIConfiguration();
  320.     
  321.     void SetDefaultFType(const TFileSpec & name) const;
  322.     void DoAutoSpin() const;
  323. private:
  324.     static Boolean firstTime;
  325.     static short    we;
  326. };
  327.  
  328. extern const GUSIConfiguration    GUSIConfig;
  329. extern SocketTable                    Sockets;
  330.  
  331. typedef pascal OSErr (*OSErrInitializer)();
  332. typedef pascal void  (*voidInitializer)();
  333.  
  334. class Feature {
  335.     Boolean    good;
  336. public:
  337.     Feature(unsigned short trapNum, TrapType tTyp);
  338.     Feature(OSType type, long value);
  339.     Feature(OSType type, long mask, long value);
  340.     Feature(const Feature & precondition, OSErrInitializer init);
  341.     Feature(OSErrInitializer init);
  342.     Feature(const Feature & precondition, voidInitializer init);
  343.     Feature(voidInitializer init);
  344.     Feature(const Feature & cond1, const Feature & cond2);
  345.  
  346.     operator void*() const {    return (void *) good;    }
  347. };
  348.  
  349. extern Feature hasMakeFSSpec;
  350. extern Feature hasAlias;
  351. extern Feature hasNewSF;
  352. extern Feature hasProcessMgr;
  353. extern Feature hasCRM;
  354. extern Feature hasCTB;
  355. extern Feature hasStdNBP;
  356. extern Feature hasCM;
  357. extern Feature hasFT;
  358. extern Feature hasTM;
  359. extern Feature    hasPPC;
  360. extern Feature hasRevisedTimeMgr;
  361.  
  362. class ScattGath    {
  363.     Handle            scratch;
  364. protected:
  365.     void *            buf;
  366.     int                        len;
  367.     int                        count;
  368.     const struct iovec *    io;
  369.  
  370.     ScattGath(const struct iovec *iov, int cnt);
  371.     virtual ~ScattGath();
  372. public:
  373.     void *            buffer()            {    return buf;            }
  374.     int                buflen()            {    return len;            }
  375.     int                length(int l)    {    return len = l;    }
  376.     operator void *()                    {    return buf;            }
  377. };
  378.  
  379. class Scatterer : public ScattGath {
  380. public:
  381.     Scatterer(const struct iovec *iov, int count);
  382.     virtual ~Scatterer();
  383. };
  384.  
  385. class Gatherer : public ScattGath {
  386. public:
  387.     Gatherer(const struct iovec *iov, int count);
  388.     virtual ~Gatherer();
  389. };
  390.  
  391. typedef pascal void (*Deferred)(void *);
  392.  
  393. class RingBuffer {
  394.     // Valid bytes are between consume and produce
  395.     // Free bytes are between produce and consume
  396.     // bytes between endbuf-spare and endbuf are neither
  397.     Ptr        buffer;
  398.     Ptr        endbuf;
  399.     Ptr         consume;
  400.     Ptr        produce;
  401.     u_short    free;
  402.     u_short    valid;
  403.     u_short    spare;
  404.     Boolean    lock;
  405.     Deferred    defproc;
  406.     void *    arg;
  407.     
  408. public:
  409.                 RingBuffer(u_short bufsiz);
  410.                 ~RingBuffer();
  411.     
  412.     Ptr        Producer(long & len);            //    Find continuous memory for producer
  413.     Ptr        Consumer(long & len);            //    Find continuous memory for consumer
  414.     void        Validate(long len);                // Validate this, unallocate rest
  415.     void         Invalidate(long len);
  416.     void        Produce(Ptr from, long & len);//    Allocate, copy & validate
  417.     void        Consume(Ptr to, long & len);    // Copy & invalidate
  418.     
  419.     long        Free()                                { return free;                                    }        
  420.     long        Valid()                                { return valid;                                }
  421.     
  422.     void         Defer()                                { lock = true;                                    }
  423.     void         Undefer()                            { lock = false; if (defproc) defproc(arg);}
  424.     Boolean    Locked()                                { return lock;                                    }
  425.     void        Later(Deferred def, void * ar){ defproc = def; arg = ar;                    }
  426.     
  427.     operator void *()                                { return buffer;                                }
  428. };
  429.  
  430. Boolean GUSIInterrupt();
  431.  
  432. Boolean CopyIconFamily(short srcResFile, short srcID, short dstResFile, short dstID);
  433.  
  434. pascal OSErr PPCInit_P();
  435.  
  436. OSErr AppleTalkIdentity(short & net, short & node);
  437.  
  438. void CopyC2PStr(const char * cstr, StringPtr pstr);
  439.  
  440. #endif